package com.example.kwokg.meishi;

import android.os.AsyncTask;

import com.example.kwokg.meishi.Bean.Content;
import com.example.kwokg.meishi.Bean.Title;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kwokg on 2016/9/16.
 */
public class LoadJsonTask extends AsyncTask<String,Void,Object> {

    private int mode;

    //使用接口回调返回Json数据
    public interface onGetJsonResultListener{
        void onGetJsonResult(Object result);
    }

    private onGetJsonResultListener mListener;

    public LoadJsonTask(onGetJsonResultListener listener,int jsonMode){
        this.mListener=listener;
        this.mode=jsonMode;
    }

    @Override
    protected Object doInBackground(String... params) {
        byte[] bytes=null;
        //使用okhttp下载json数据，因为就是在异步任务内所以okhttp不再使用异步任务
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder().url(params[0]).build();
        try {
            Response response = client.newCall(request).execute();
            bytes = response.body().bytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (bytes==null){
            return null;
        }
        String json=new String(bytes);
        try {
            return jsonToData(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    protected void onPostExecute(Object data) {
        super.onPostExecute(data);
        if (mListener!=null&&data!=null){
            mListener.onGetJsonResult(data);
        }

    }

    //把Json数据转换为data
    private Object jsonToData(String json) throws JSONException {
        switch (mode){
            case Constants.TITLE:
                JSONArray titleArray=new JSONArray(json);
                List<Title> titles=new ArrayList<>();
                for ( int i = 0 ; i < titleArray.length() ; i++){
                    JSONObject titleObj=titleArray.getJSONObject(i);
                    Title title=new Title();
                    title.setId(titleObj.getInt("id"));
                    title.setIndex(titleObj.getInt("index"));
                    title.setType(titleObj.getInt("type"));
                    title.setName(titleObj.getString("name"));
                    titles.add(title);
                }

                return titles;
            case Constants.CONTENT:
                JSONArray outArray=new JSONArray(json);
                List<Content> contents=new ArrayList<>();
                for ( int i = 0 ; i < outArray.length() ; i++){
                    JSONObject outObj=outArray.getJSONObject(i);
                    if (outObj.has("media")){
                        Content content=new Content();
                        content.setRecommend_caption(outObj.getString("recommend_caption"));
                        JSONObject midObj=outObj.getJSONObject("media");
                        content.setCover_pic(midObj.getString("cover_pic"));
                        content.setLikes_count(midObj.getInt("likes_count"));
                        content.setCover_pic(midObj.getString("cover_pic"));
                        content.setVideo(midObj.getString("video"));
                        JSONObject lastObj=midObj.getJSONObject("user");
                        content.setAvatar(lastObj.getString("avatar"));
                        contents.add(content);
                    }
                }
                return contents;
        }
        return null;
    }
}
